package com.boc.langxiang.ui.activity.me;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.boc.langxiang.App;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.me.AccountSetContract;
import com.boc.langxiang.entity.events.LogoutEvent;
import com.boc.langxiang.entity.response.AccountUpdateInfoContent;
import com.boc.langxiang.entity.response.UserInfo;
import com.boc.langxiang.manager.DataCleanManager;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.presenter.me.AccountSetPresenter;
import com.boc.langxiang.ui.activity.common.AddressManageActivity;
import com.boc.langxiang.ui.activity.common.RegistAndLoginActivity;
import com.boc.langxiang.ui.view.SongTextView;
import com.boc.langxiang.util.DialogUtil;
import com.boc.langxiang.util.T;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 编辑资料
 */
public class AccountSetActivity extends MeBaseToolBarActivity implements AccountSetContract.View, Handler.Callback {


    @BindView(R.id.stv_me_set_nickname)
    SongTextView stvMeSetNickname;
    @BindView(R.id.stv_me_set_gender)
    SongTextView stvMeSetGender;
    @BindView(R.id.stv_me_set_phonenumber)
    SongTextView stvMeSetPhonenumber;
    private Handler mHandler;
    private AccountSetPresenter mPresenter;

    private String mTotalCacheSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_set);
        ButterKnife.bind(this);
        setToolBarTitle("编辑资料");
        mHandler = new Handler((Handler.Callback) this);
        try {
            mTotalCacheSize = DataCleanManager.getTotalCacheSize(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mPresenter == null) {
            mPresenter = new AccountSetPresenter(this);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isLogin()) {
            UserInfo userInfo = UserInfoManager.getInstance().getUserInfo();
            if(userInfo!=null){
                stvMeSetNickname.setText(userInfo.getNickname());
                stvMeSetPhonenumber.setText(userInfo.getPhone());
                stvMeSetGender.setText(userInfo.getSex());
            }
        }else{
            finish();
        }
    }

    @Override
    protected int setToolBarBackgroundColor() {
        return Color.WHITE;
    }

    @Override
    protected int setToolBarBackImg() {
        return R.drawable.icon_me_left;
    }

    @Override
    protected int setToolBarTitleColor() {
        return Color.BLACK;
    }

    @OnClick({R.id.mvi_me_set_modifypwd, R.id.mvi_me_set_addressmanager, R.id.relayout_me_set_modifyname,
            R.id.btn_me_set_exitlogin, R.id.relayout_me_set_gender, R.id.mvi_me_set_cleancache})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mvi_me_set_addressmanager:
                startActivity(new Intent(this, AddressManageActivity.class));
                break;
            case R.id.mvi_me_set_modifypwd:
                startActivity(new Intent(this, SetModifyPasswordActivity.class));
                break;
            case R.id.relayout_me_set_modifyname:
                startActivity(new Intent(this, SetModifyNickNameActivity.class));
                break;
            case R.id.btn_me_set_exitlogin:
                UserInfoManager.getInstance().clearData();
//                startActivity(new Intent(mContext, RegistAndLoginActivity.class));
                EventBus.getDefault().post(new LogoutEvent());
                finish();
                break;
            case R.id.relayout_me_set_gender:
                startActivity(new Intent(mContext, SetModifyGenderActivity.class));

                break;
            case R.id.mvi_me_set_cleancache:
                if ("0k".equalsIgnoreCase(mTotalCacheSize)) {
                    T.showShort(AccountSetActivity.this, "不需要清理!");
                    return;
                }

                final Dialog progressDialog = DialogUtil.createProgressDialog(AccountSetActivity.this, "正在清理缓存...");
                progressDialog.show();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        boolean b = DataCleanManager.clearAllCache(AccountSetActivity.this);
                        if (b) {
                            mHandler.sendEmptyMessage(100);
                        }
                        progressDialog.dismiss();
                    }
                }).start();

          /*      Observable<Object> newMethod = newMethod(progressDialog);
                newMethod.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io()).subscribe(new Subscriber<Object>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("AccountSetActivity", e.getMessage());
                    }

                    @Override
                    public void onNext(Object o) {
                        Log.d("AccountSetActivity", "success");
                    }
                });*/
                break;
        }
    }

/*
    private Object cleanData(Dialog progressDialog) {
        boolean b = DataCleanManager.clearAllCache(AccountSetActivity.this);
        Log.d("AccountSetActivity", "b:" + b);
        progressDialog.dismiss();
        Toast.makeText(App.getInstance(), "清理成功", Toast.LENGTH_SHORT).show();
        return new String();
    }

    public Observable<Object> newMethod(final Dialog progressDialog) {
        return Observable.defer(new Func0<Observable<Object>>() {
            @Override
            public Observable<Object> call() {
                return Observable.just(cleanData(progressDialog));
            }
        });
    }
*/

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(String code, String msg) {
        Toast.makeText(App.getInstance(), msg, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void getAccountUpdateInfoContent(AccountUpdateInfoContent content) {
        String sex = content.getSex();
        String nickname = content.getNickname();
        String phone = content.getPhone();
        stvMeSetNickname.setText(nickname);
        stvMeSetPhonenumber.setText(phone);
        stvMeSetGender.setText(sex);
    }

    @Override
    public boolean handleMessage(Message msg) {
        T.showShort(AccountSetActivity.this, "缓存清理完成!");
        return false;
    }

}
